/**
 * @class   wosh::persistence::raw::EncoderProtocolRaw
 * @brief   
 *
 ****************************************************************************
 * @version $Id: EncoderProtocolRaw.h 2739 2010-07-07 16:29:01Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Persistence_Raw_EncoderProtocolRaw_H__
 #define __WOSH_Persistence_Raw_EncoderProtocolRaw_H__

 #include <woshDefs.h>
 #include <core/EncoderProtocol.h>
 #include <core/PersistenceManager.h>
 #include <stdio.h>


namespace wosh {
 namespace persistence {
  namespace raw {



#undef WOSH_ENCODER_RAW_CLASS
#define WOSH_ENCODER_RAW_CLASS(CLASSNAME, OBJECT_TYPE)		\
	class CLASSNAME : public wosh::persistence::raw::EncoderBaseRaw {														\
		public: virtual inline bool isAbstract() const { return false; }													\
		public: static inline const char* className() { return "wosh::persistence::raw::" #CLASSNAME; }						\
		public: virtual inline const char* getClassName() const { return className(); }										\
		public: static inline const char* mappedObject() { return #OBJECT_TYPE; }											\
		public: virtual inline const char* getMappedObject() const { return mappedObject(); }								\
		public: inline WRESULT serializeTo( const ISerializable* object_s, FILE* fout, int encoder_options ) {				\
			return CLASSNAME::serializeTo( dynamic_cast<const OBJECT_TYPE*>(object_s), fout, encoder_options); }		\
		public: inline WRESULT deserializeFrom( ISerializable* object_s, FILE* fin ) {										\
			return CLASSNAME::deserializeFrom( dynamic_cast<OBJECT_TYPE*>(object_s), fin); }								\
		public: static WRESULT serializeTo( const OBJECT_TYPE* object, FILE* fout, int encoder_options );					\
		public: static WRESULT deserializeFrom( OBJECT_TYPE* object, FILE* fin );											\
		public: CLASSNAME() : EncoderBaseRaw() { }																			\
	};


#undef WOSH_ENCODER_RAW_BIND
#define WOSH_ENCODER_RAW_BIND(ENCODER_CLASS, OBJECT_CLASSNAME, VERSION, TMP ) \
	WOSH_ENCODER_BIND(wosh::persistence::raw::EncoderProtocolRaw, ENCODER_CLASS, OBJECT_CLASSNAME, VERSION, TMP )



class EncoderBaseRaw : public virtual EncoderBase
 {
	WOSH_CLASSNAME(wosh::persistence::raw::EncoderBaseRaw)
	WOSH_CLASS_ABSTRACT(true)

	public:
		virtual WRESULT serializeTo( const ISerializable* object, FILE* fout, int encoder_options ) = 0;
		virtual WRESULT deserializeFrom( ISerializable* object, FILE* fin ) = 0;

	public:
		virtual int64 serializeTo( const ISerializable* object, char* buffer, int64 buffer_size, int encoder_options );
		virtual int64 deserializeFrom( ISerializable* object, const char* buffer, int64 buffer_size );


	public:
		virtual ~EncoderBaseRaw() { }

}; // class def


// ------------------------------------------------------------------------------------------------------------------------

class EncoderProtocolRaw : public EncoderProtocol
 {
	WOSH_ENCODERPROTOCOL(wosh::persistence::raw::EncoderProtocolRaw)

	public:
		EncoderProtocolRaw();
		virtual ~EncoderProtocolRaw() { }

	public:
		const char* getProtocol() const		{ return "RAW"; }
		double getProtocolVersion() const	{ return 0.0; }

		PersistenceDb* createDb() const;

	public:
		WRESULT loadObjects( std::vector<ISerializable*>& objects, const std::string& parameters );

		WRESULT saveObject( const ISerializable* object, const std::string& parameters );
		WRESULT saveObjects( const std::vector<ISerializable*>& objects, const std::string& parameters );

	public:
		static WRESULT serializeTo( const ISerializable* object, FILE* fout, int encoder_options );
		static WRESULT deserializeFrom( ISerializable* object, FILE* fin );
		static ISerializable* deserializeFrom(  FILE* fin, WRESULT* return_code = NULL );


//		virtual WRESULT serializeTo( const PersistenceDb* object );

		template <class DYNAMIC_CAST>
		static inline DYNAMIC_CAST* deserializeFrom(  FILE* fin, WRESULT* return_code = NULL ) {
			return dynamic_cast<DYNAMIC_CAST*>( deserializeFrom(fin, return_code) );
		 }


}; // class def

// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------

class PersistenceDbRaw : public PersistenceDb
{
	WOSH_CLASSNAME(wosh::persistence::raw::PersistenceDbRaw)
	WOSH_CLASS_ABSTRACT(false)

	public:
		PersistenceDbRaw() : PersistenceDb() { }
		virtual ~PersistenceDbRaw() { }

	public:
		virtual inline const char* getProtocol() const		{ return "RAW"; }
		virtual inline double getProtocolVersion() const	{ return 0.0; }

		WRESULT save() const;
		WRESULT load();

}; // class def

// ------------------------------------------------------------------------------------------------------------------------

  }; // namespace raw
 }; // namespace persistence
}; // namespace wosh

#endif //__WOSH_Persistence_Raw_EncoderProtocolRaw_H__
